//package io.github.novaframe.config;
//
//import io.github.novaframe.web.filter.CachingHttpHeadersFilter;
//import io.undertow.UndertowOptions;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.web.server.MimeMappings;
//import org.springframework.boot.web.server.WebServerFactory;
//import org.springframework.boot.web.server.WebServerFactoryCustomizer;
//import org.springframework.boot.web.servlet.ServletContextInitializer;
//import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.env.Environment;
//
//import javax.servlet.DispatcherType;
//import javax.servlet.FilterRegistration;
//import javax.servlet.ServletContext;
//import javax.servlet.ServletException;
//import java.io.File;
//import java.nio.file.Paths;
//import java.util.EnumSet;
//
///**
// * ServletContextInitializer: Servlet初始化器
// * EmbeddedServletContainerCustomizer: Servlet容器配置
// *
// * Configuration of web application with Servlet 3.0 APIs.
// */
//@Configuration
//public class WebConfiguration implements ServletContextInitializer, WebServerFactoryCustomizer<WebServerFactory> {
//
//    private final Logger logger = LoggerFactory.getLogger(getClass());
//
//    private final Environment env;
//    private final NovaframeProperties novaframeProperties;
////    private MetricRegistry metricRegistry;
//
//    @Autowired
//    public WebConfiguration(Environment env, NovaframeProperties novaframeProperties) {
//        super();
//        this.env = env;
//        this.novaframeProperties = novaframeProperties;
//    }
//
////    @Autowired(required = false)
////    public void setMetricRegistry(MetricRegistry metricRegistry) {
////        this.metricRegistry = metricRegistry;
////    }
//
//    /**
//     * Customize the Servlet engine: Mime types, the document root, the cache.
//     */
//    @Override
//    public void customize(ConfigurableEmbeddedServletContainer container) {
//        MimeMappings mappings = new MimeMappings(MimeMappings.DEFAULT);
//        // IE issue, see https://github.com/jhipster/generator-jhipster/pull/711
//        mappings.add("html", "text/html;charset=utf-8");
//        // CloudFoundry issue, see https://github.com/cloudfoundry/gorouter/issues/64
//        mappings.add("json", "text/html;charset=utf-8");
//        container.setMimeMappings(mappings);
//        // When running in an IDE or with ./mvnw spring-boot:run, set location of the static web assets.
//        setLocationForStaticAssets(container);
//
//        /*
//         * Enable HTTP/2 for Undertow - https://twitter.com/ankinson/status/829256167700492288
//         * HTTP/2 requires HTTPS, so HTTP requests will fallback to HTTP/1.1.
//         * See the JHipsterProperties class and your application-*.yml configuration files
//         * for more information.
//         */
//        if (novaframeProperties.getHttp().getVersion().equals(NovaframeProperties.Http.Version.V_2_0) &&
//                container instanceof UndertowEmbeddedServletContainerFactory) {
//
//            ((UndertowEmbeddedServletContainerFactory) container)
//                    .addBuilderCustomizers(builder ->
//                            builder.setServerOption(UndertowOptions.ENABLE_HTTP2, true));
//        }
//    }
//
//    @Override
//    public void onStartup(ServletContext servletContext) throws ServletException {
//        if (env.getActiveProfiles().length != 0) {
//            logger.info("Web application configuration, using profiles: {}", (Object[]) env.getActiveProfiles());
//        }
//        EnumSet<DispatcherType> disps = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.ASYNC);
//
//        //initMetrics(servletContext, disps);
//
//        if (env.acceptsProfiles(NovaframeConstants.SPRING_PROFILE_PRODUCTION)) {
//            initCachingHttpHeadersFilter(servletContext, disps);
//        }
//        logger.info("Web application fully configured");
//    }
//
//    /**
//     * Initializes Metrics.
//     */
////    private void initMetrics(ServletContext servletContext, EnumSet<DispatcherType> disps) {
////        logger.debug("Initializing Metrics registries");
////        servletContext.setAttribute(InstrumentedFilter.REGISTRY_ATTRIBUTE, metricRegistry);
////        servletContext.setAttribute(MetricsServlet.METRICS_REGISTRY, metricRegistry);
////
////        logger.debug("Registering Metrics Filter");
////        FilterRegistration.Dynamic metricsFilter = servletContext.addFilter("webappMetricsFilter",
////                new InstrumentedFilter());
////
////        metricsFilter.addMappingForUrlPatterns(disps, true, "/*");
////        metricsFilter.setAsyncSupported(true);
////
////        logger.debug("Registering Metrics Servlet");
////        ServletRegistration.Dynamic metricsAdminServlet =
////                servletContext.addServlet("metricsServlet", new MetricsServlet());
////
////        metricsAdminServlet.addMapping("/management/metrics/*");
////        metricsAdminServlet.setAsyncSupported(true);
////        metricsAdminServlet.setLoadOnStartup(2);
////    }
//
//    @SuppressWarnings("unused")
//    private void setLocationForStaticAssets(ConfigurableEmbeddedServletContainer container) {
//        File root;
//        String prefixPath = resolvePathPrefix();
//        root = new File(prefixPath + "target/www/");
//        if (root.exists() && root.isDirectory()) {
//            container.setDocumentRoot(root);
//        }
//    }
//
//    /**
//     *  Resolve path prefix to static resources.
//     */
//    private String resolvePathPrefix() {
//        String fullExecutablePath = this.getClass().getResource("").getPath();
//        String rootPath = Paths.get(".").toUri().normalize().getPath();
//        String extractedPath = fullExecutablePath.replace(rootPath, "");
//        int extractionEndIndex = extractedPath.indexOf("target/");
//        if(extractionEndIndex <= 0) {
//            return "";
//        }
//        return extractedPath.substring(0, extractionEndIndex);
//    }
//
//    /**
//     * Initializes the caching HTTP Headers Filter.
//     */
//    private void initCachingHttpHeadersFilter(ServletContext servletContext,
//                                              EnumSet<DispatcherType> disps) {
//        logger.debug("Registering Caching HTTP Headers Filter");
//        FilterRegistration.Dynamic cachingHttpHeadersFilter =
//                servletContext.addFilter("cachingHttpHeadersFilter",
//                        new CachingHttpHeadersFilter(novaframeProperties));
//
//        cachingHttpHeadersFilter.addMappingForUrlPatterns(disps, true, "/content/*");
//        cachingHttpHeadersFilter.addMappingForUrlPatterns(disps, true, "/app/*");
//        cachingHttpHeadersFilter.setAsyncSupported(true);
//    }
//
//    @Override
//    public void customize(WebServerFactory factory) {
//
//    }
//}
